import apiClient from '@/api/axios.js';

/**
 * 用户资料API类
 *
 * 用户数据结构:
 * {
 *   userId: Number,        // 用户ID
 *   username: String,      // 用户名
 *   email: String,         // 邮箱
 *   creditScore: Number,   // 信用分数
 *   createTime: String,    // 创建时间
 *   status: String,        // 状态
 *   headshot: String       // 头像URL
 * }
 *
 * 聊天室数据结构:
 * {
 *   chatSessionID: Number, // 聊天室ID
 *   user1ID: Number,       // 用户1 ID
 *   user2ID: Number,       // 用户2 ID
 *   creationTime: String   // 创建时间
 * }
 *
 * 消息数据结构:
 * {
 *   messageID: Number,     // 消息ID
 *   chatSessionID: Number, // 聊天室ID
 *   senderID: Number,      // 发送者ID
 *   content: String,       // 消息内容
 *   sendTime: String,      // 发送时间
 *   status: String         // 消息状态 (sending, sent, read)
 * }
 *
 * 消息列表响应结构:
 * {
 *   status: String,        // 状态
 *   messages: Array,       // 消息数组
 *   totalMessages: Number, // 总消息数
 *   pageNum: Number,       // 当前页码
 *   pageSize: Number       // 每页大小
 * }
 */
export class UserProfileApi {
    // 根据用户ID查询商品收藏
    static async viewItemCollections(userId, pageNum = 1, pageSize = 20) {
        try {
            const response = await apiClient.get('/user-profile/view-itemcollections', {
                params: { userID: userId, PageNum: pageNum, PageSize: pageSize },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回商品收藏列表
        } catch (error) {
            throw new Error(error.message || '获取商品收藏失败');
        }
    }

    // 根据用户ID查询灾害
    static async getDisasters(userId) {
        try {
            const response = await apiClient.get('/user-profile/disasters', {
                params: { userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回灾害列表
        } catch (error) {
            throw new Error(error.message || '获取灾害信息失败');
        }
    }

    // 根据用户ID获取二手物品
    static async getSecondHandItems(userId) {
        try {
            const response = await apiClient.get('/user-profile/secondhanditems', {
                params: { userID: userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回二手商品列表
        } catch (error) {
            throw new Error(error.message || '获取二手商品失败');
        }
    }

    // 用户点开聊天室后将当前所有消息标记为已读
    static async markMessagesAsRead(chatRoomId, userId) {
        try {
            const response = await apiClient.post(
                '/user-profile/markasread',
                {},
                {
                    params: { chatRoomId, userID: userId },
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回标记结果
        } catch (error) {
            throw new Error(error.message || '标记消息为已读失败');
        }
    }

    // 分页获取聊天室内的消息
    static async getChatMessages(chatRoomId, pageNum = 1, pageSize = 10) {
        try {
            const response = await apiClient.get('/user-profile/messages', {
                params: { chatRoomId, pageNum, pageSize },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回聊天消息列表
        } catch (error) {
            throw new Error(error.message || '获取聊天消息失败');
        }
    }

    // 发送消息到指定的聊天室
    static async sendMessage(chatRoomId, senderId, content) {
        try {
            const response = await apiClient.post(
                '/user-profile/send',
                {},
                {
                    params: { chatRoomId, senderId, content },
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回发送结果
        } catch (error) {
            throw new Error(error.message || '发送消息失败');
        }
    }

    // 查询聊天室的第一条消息
    static async getFirstMessage(chatRoomId) {
        try {
            const response = await apiClient.get('/user-profile/firstmessage', {
                params: { chatRoomId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回第一条消息
        } catch (error) {
            throw new Error(error.message || '获取第一条消息失败');
        }
    }

    // 创建聊天室
    static async createChatRoom(user1ID, user2ID) {
        try {
            const response = await apiClient.post(
                '/user-profile/create',
                {},
                {
                    params: { user1ID, user2ID },
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回创建结果
        } catch (error) {
            throw new Error(error.message || '创建聊天室失败');
        }
    }

    // 根据用户ID查询聊天室
    static async getChatRooms(userId) {
        try {
            const response = await apiClient.get('/user-profile/chatroom', {
                params: { userId: userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回聊天室列表
        } catch (error) {
            throw new Error(error.message || '获取聊天室失败');
        }
    }

    // 根据文章ID获取文章的收藏数量和评论数量
    static async getArticleStats(articleId) {
        try {
            const response = await apiClient.get('/user-profile/stats', {
                params: { articleId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回文章统计信息
        } catch (error) {
            throw new Error(error.message || '获取文章统计信息失败');
        }
    }

    // 根据用户ID获取已发布文章数量
    static async getPublishedArticleCount(userId) {
        try {
            const response = await apiClient.get('/user-profile/article/count', {
                params: { userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回文章数量
        } catch (error) {
            throw new Error(error.message || '获取文章数量失败');
        }
    }

    // 取消关注
    static async unfollowUser(followerId, followedId) {
        try {
            const response = await apiClient.delete('/user-profile/unfollow', {
                params: { followerId, followedId },
                headers: {
                    'Content-Type': 'multipart/form-data',
                },
            });
            return response.data; // 返回取消关注结果
        } catch (error) {
            throw new Error(error.message || '取消关注失败');
        }
    }
    // 根据用户ID获取个人信息
    static async getUserInfo(userId) {
        try {
            const response = await apiClient.get('/user-profile/info', {
                params: { userId: userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回用户信息
        } catch (error) {
            throw new Error(error.message || '获取用户信息失败');
        }
    }

    // 获取自己关注了多少人
    static async getFollowingCount(userId) {
        try {
            const response = await apiClient.get('/user-profile/following/count', {
                params: { userId: userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回关注人数
        } catch (error) {
            throw new Error(error.message || '获取关注人数失败');
        }
    }

    // 获取有多少人关注自己
    static async getFollowerCount(userId) {
        try {
            const response = await apiClient.get('/user-profile/follow/count', {
                params: { userId: userId },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回粉丝人数
        } catch (error) {
            throw new Error(error.message || '获取粉丝人数失败');
        }
    }

    // 修改文章
    static async modifyArticle(articleID, title, content) {
        try {
            const response = await apiClient.post(
                '/user-profile/modify-article',
                { articleID, title, content },
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回修改结果
        } catch (error) {
            throw new Error(error.message || '修改文章失败');
        }
    }

    // 根据用户ID查询文章
    static async viewPublishedArticles(userId, pageNum = 1, pageSize = 10) {
        try {
            const response = await apiClient.get('/user-profile/view-published-articles', {
                params: { userID: userId, PageNum: pageNum, PageSize: pageSize },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回文章列表
        } catch (error) {
            throw new Error(error.message || '获取文章列表失败');
        }
    }

    // 关注用户
    static async followUser(targetUserID, token) {
        try {
            const response = await apiClient.post(
                '/user-profile/follow',
                { targetUserID },
                {
                    headers: {
                        'Content-Type': 'multipart/form-data',
                        'token': token,
                    },
                }
            );
            return response.data; // 返回关注结果
        } catch (error) {
            throw new Error(error.message || '关注用户失败');
        }
    }

    // 根据用户ID查看文章收藏
    static async viewCollections(userId, pageNum = 1, pageSize = 20) {
        try {
            const response = await apiClient.get('/user-profile/view-collections', {
                params: { userID: userId, PageNum: pageNum, PageSize: pageSize },
                headers: {
                    'Content-Type': 'application/json',
                },
            });
            return response.data; // 返回文章收藏列表
        } catch (error) {
            throw new Error(error.message || '获取文章收藏失败');
        }
    }

    // 更新用户信息
    static async updateProfile(userId, username, email, headshot, password, status) {
        try {
            const response = await apiClient.post(
                '/user-profile/update-profile',
                { userID: userId, username, email, headshot, status },
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回更新结果
        } catch (error) {
            throw new Error(error.message || '更新用户信息失败');
        }
    }

    // 用户登录
    static async login(username, password) {
        try {
            const response = await apiClient.post(
                '/user-profile/login',
                { username, password },
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回登录结果（包括 token）
        } catch (error) {
            throw new Error(error.message || '登录失败');
        }
    }

    // 用户注册
    static async register(username, password, email, headshot='', status='normal') {
        try {
            const response = await apiClient.post(
                '/user-profile/register',
                { username, password, email, headshot, status },
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回注册结果
        } catch (error) {
            throw new Error(error.message || '注册失败');
        }
    }

    // 管理员登录
    static async loginAdmin(username, password) {
        try {
            const response = await apiClient.post(
                '/user-profile/login_admin',
                {
                    username: username,
                    password: password
                },
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            return response.data; // 返回登录结果
        } catch (error) {
            throw new Error(error.message || '管理员登录失败');
        }
    }
}
